package com.wangmin.intercept.adapter;

import android.graphics.Bitmap;
import android.webkit.WebResourceResponse;

import com.wangmin.intercept.listener.IWebView;
import com.wangmin.intercept.listener.IWebViewClient;
import com.wangmin.intercept.listener.UrlAddedListener;
import com.wangmin.intercept.utils.LogUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Timer;


/**
 * Created by wm on 2017/3/9.
 */

public class WebviewClientAdater implements IWebViewClient {
    protected String kyContent;
    protected String kyTitle;
    private Timer timer = new Timer();
    private LogUtils logUtils;
    private boolean needClearHistory = false;
    private IWebView iWebView;
    private List<String> urls;
    private UrlAddedListener urlAddedListener;
    private boolean isStart = false;
    private List<String> addInterceptUrl;
    public WebviewClientAdater(UrlAddedListener urlAddedListener){
        urls = new ArrayList<>();
        logUtils = LogUtils.getNewInstance("WebviewClientAdater");
        this.urlAddedListener = urlAddedListener;
        addInterceptUrl = new ArrayList<>();
    }
    @Override
    public void onPageFinished(final IWebView view,String url){
        this.iWebView = view;
        if(urlAddedListener!=null){
//            SdkInit.getUiHandle().post(new Runnable() {
//                @Override
//                public void run() {
//                    view.clearHistory();
//                }
//            });
            List<String> list = getUrls();
            urlAddedListener.addFinished(list);
        }
    }

    @Override
    public void onPageStarted(IWebView view, String url, Bitmap favicon) {
        isStart = false;
        urls.clear();
        isStart = true;
        if(urlAddedListener!=null){
            urlAddedListener.beginAdd();
        }
    }

    @Override
    public WebResourceResponse shouldInterceptRequest(IWebView view, String url) {
        logUtils.dob("开始拦截url"+url);
        if(isStart) {
            urls.add(url);
            if (urlAddedListener != null) {
                urlAddedListener.adding(url);
            }
            if(containsUrls(url)){
                logUtils.dob("开始拦截url"+url);
                return new WebResourceResponse("text/json","",null);
            }
        }
        return null;
    }

    /**
     * 广告拦截新接口
     * @param url
     * @return
     */
    public boolean containsUrls(String url){
        boolean contains = false;
        for(String adInfo:addInterceptUrl){
            if(url.contains(adInfo)){
                contains = true;
                break;
            }
        }
        return contains;
    }

    @Override
    public void onDestory() {
        if(timer!=null){
            timer.cancel();
            timer = null;
        }
    }

    @Override
    public void doUpdateVisitedHistory(IWebView view, String url, boolean isReload) {
        if(needClearHistory){
            needClearHistory = false;
            view.clearHistory();
        }
    }

    @Override
    public void onStart() {
       onDestory();

    }

    public List<String> getUrls() {
        List<String> list = new ArrayList<>();
        Iterator<String> iterator = urls.listIterator();
        while(iterator.hasNext()){
            String url = iterator.next();
            list.add(url);
        }
        return list;
    }

    public void clear(){
        addInterceptUrl.clear();
    }

    public void addUrl(String url){
        if(url!=null){
            addInterceptUrl.add(url);
        }
    }

    public void addUrl(List<String> url){
        if(url!=null){
            addInterceptUrl.addAll(url);
        }
    }
}
